<?php
include_once(LIB_PATH."/InputFilter.php");
class comment_model_state extends Model {
	
	public function __construct(){
		parent::__construct();
		$this->table = 'app_com_state';
	}
	
	//写入状态数据
	public function insert_data($post){	
			
		$Filter = new InputFilter();
		
		$sql 	= " select id from " . $this->table . " where uid = " . $post['uid'] . " and last_one = 1";
		$result = $this->db->fetchRow($sql);
		$old_state_id = $result['id'] ;
		$arguments = filter_params($post,'feel','state','uid','username');
		trim_params($arguments);
		$arguments['state'] = $Filter->process($arguments['state']);
		$arguments['type'] = 0;
		$arguments['srank'] = 0;
		$arguments['create_time'] = time();
		$state_id = $this->db->insert($this->table,$arguments);

		return $state_id;
		
	}
	
	//读取一条状态信息
	public function read_a_data($uid){
		$sql = "select uid,username,state,create_time,feel from ".$this->table." where uid=&uid and type=&type and srank=&srank and `delete`=0 order by create_time desc";
		$params = array('uid'=>$uid,'type'=>0,'srank'=>0);
		return $this->db->fetchRow($sql,$params);
	}
	
	//分页读取状态信息
	public function read_page_data($uid,$page,$page_limit){
		$sql = "select uid,id,feel,state,create_time from ".$this->table." where uid=&uid and type=&type and srank=&srank and `delete`=0 order by create_time desc";
		$params = array('uid'=>$uid,'type'=>0,'srank'=>0);
		return $result = $this->db->selectPage($sql,$params,$page,$page_limit);
		
	}
	//查询一条状态信息
	public function get_a_state($state_id){
		
		$sql = "select * from ".$this->table." where id=&id and type=&type and srank=&srank and `delete`=0 ";
		$params = array('id'=>$state_id,'type'=>0,'srank'=>0);
		
		return $this->db->fetchRow($sql,$params);
	}
	
	public function count_state($uid){
		$sql = "select count(*) from ".$this->table." where uid=&uid and type=&type and srank=&srank and `delete`=0 ";
		$params = array('uid'=>$uid,'type'=>0,'srank'=>0);
		$result  = $this->db->fetchRow($sql,$params);
		return $result['count(*)'];
	}
	
	//删除状态信息
	public function delete_data($login_uid,$id){
		//查询信息
		$state_data = $this->get_a_state($id);

		if($state_data['uid'] == $login_uid){
			
			$sql = "update " . $this->table . " set `delete`=1  where `id` = $id ";
			$result = $this->db->query($sql);
		
			$sql = "select id from ".$this->table." where uid=&uid and type=&type and srank=&srank and `delete`=0 order by create_time desc";
			$params = array('uid'=>$login_uid,'type'=>0,'srank'=>0);
			$new_state_id =  $this->db->fetchRow($sql,$params);
			
			if($new_state_id) $this->update_state_id($new_state_id['id']);
			
			return true;
			
		}else{
			return false;
		}
		
			
	}
	
	//更新最新的状态的id
	public function update_state_id($state_id){
		
		$sql = "update " . $this->table . " set last_one = 1  where id = " . $state_id ;
		$this->db->query($sql);	
//		$sql = "update  app_attention set state_id= " . $state_id . " where uid = " . $uid ;		
//		$this->db->query($sql);
	}
	
	//得到好友的最新的状态
	public function get_friend_new_data($uid,$page,$page_limit){

		$sql = "select s.* from ".$this->table." s  left join 
				app_attention f on s.id = f.state_id 
				where f.host_id =&uid and s.type=0 and s.srank=0 and f.is_friend=1 and `delete`=0
				order by s.create_time desc";		
		$sql = "select id from " . $this->table . " where  uid in ( " . $s . ")  order by create_time desc" ;	
		$params = array('uid'=>$uid);
		$friend_data = $this->db->selectPage($sql,$params,$page,$page_limit);
		return $friend_data;
	}
	
	//更新评论数
	public function update_comment_num($id){
		$sql = "update ".$this->table." set comment_num=comment_num+1 where id=".$id;
		return $this->db->query($sql);
	}
	
	//得到一定时间内的用户心情条数
	public function getCountTime($uid,$time){
		$sql = "select count(*)  from ".$this->table." where uid=&uid and `delete`=0 and create_time > '".$time."'";
		$params = array('uid'=>$uid);
		$result = $this->db->fetchRow($sql,$params);
		if($result['count(*)'] == '') $result['count(*)'] =0;
		return $result['count(*)'];
	}
	
	//我的纪录相关操作
	public function insert_record_data($post){
		$arguments = filter_params($post,'srank','state','uid');
		trim_params($arguments);
		$arguments['type'] = 1;
		$arguments['feel'] = 0;
		$arguments['create_time'] = time();
		return  $this->db->insert($this->table,$arguments);
	}
	
	public function read_record_data($uid){
			$sql = "select uid,id,state,srank,create_time from ".$this->table." where uid=&uid and type=&type and `delete`=0 order by create_time desc";
			$params = array('uid'=>$uid,'type'=>1);
			return $this->db->fetchALL($sql,$params);
	}
	
	public function read_record_data_id($id){
			$sql = "select uid,id,state,srank,create_time from ".$this->table." where id=&id and type=&type and `delete`=0 order by create_time desc";
			$params = array('id'=>$id,'type'=>1);
			return $this->db->fetchRow($sql,$params);
	}
	
	public function update_record_data($post){
			$sql =  "update ".$this->table." set state='".$post['state']."',srank='".$post['srank']."' where id='".$post['id']."'";
			return $this->db->query($sql);
	}
	
	
	//行缓存函数  //取得用户state 所有state_id 列表
	public function getStateListId($uid){
		
			$sql = "select id from  " . $this->table . " where   `uid` = $uid  and `delete`=0 order by create_time desc";			
			$result = $this->db->fetchALL($sql);
				
			foreach($result as $row){
				$array[] = $row['id'];
			}
			return $array;
	}
	
	//根据id列表取得用户的数据
	public function getRow($ids){
		
		$ids = (array) $ids;
		$ids = implode(',', $ids);
		return $this->db->fetchAll("SELECT * FROM " . $this->table . " WHERE  `delete`=0 and id IN({$ids})");
		
	}
	
	//好友最新状态的id列表
	public function get_friend_new_data_id($uid){
		
		$fid = friends_util_friendutil::getFriendIds($uid);	
    	if(!empty($fid)){
			$s = implode(',',$fid);
			$s = '0,'.$s;			
			$sql = "select id from " . $this->table . " where  uid in ( " . $s . ")  order by create_time desc" ;			
			$result = $this->db->fetchAll($sql,$params);		
			foreach($result as $row){
					$array[] = $row['id'];
				}
			return $array;
		} else {
			return array();
		}
		
	}
	
	//sms 取得数据接口
	public function getSmsDataById($id){		
			$cacheKey =  md5('getSmsDataById'.$id);
			if (!$data = Cache::getInstance()->load($cacheKey)){		
				 $sql = "select * from " . $this->table. " where id = " . $id  ;
				 $data = $this->db->fetchRow($sql);
			  	 Cache::getInstance()->save($data, $cacheKey, array(), 3600000);
		    	     	 
			} else {
				 $data = Cache::getInstance()->load($cacheKey);
			}
			
			return $data;			
			
	}
	

	
}

